﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports HtmlAgilityPack

Partial Class Control_CertifiedHousehold_UpLoad
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then



        End If
    End Sub
    Private Sub BindToGrid()
        Using data As New VSEntities
            'So ban ghi muon the hien tren trang
            Dim intPag_Size As Int32 = drpPage_Size.SelectedValue
            Dim p = (From q In data.CertifiedHouseholds Where q.ProvinceId = txtTitleFilter.Text And q.Ref2 = 0 Select q).ToList
            Dim strKey_Name() As String = {"CertifiedID", "CertifiedIdOld"}
            'Tong so ban ghi

            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub

#End Region
#Region "Event for control"

    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then


            Using Data As New VSEntities
                Dim _CertifiedID = grdShow.DataKeys(e.Row.RowIndex)("CertifiedID").ToString
                Dim p = (From q In Data.CertifiedHouseholds Where q.CertifiedID = _CertifiedID Select q).FirstOrDefault
                Try
                    Dim _thuongtru As String() = p.Ow_Resident.ToString.Split(",")
                    Dim _tamtru As String() = p.Ow_Present.ToString.Split(",")


                    Dim _tinh As String = _thuongtru(_thuongtru.Length - 1).Trim()
                    Dim _Huyen As String = _thuongtru(_thuongtru.Length - 2).Trim()
                    Dim _Xa As String = _thuongtru(_thuongtru.Length - 3).Trim()

                    'thường trú
                    Dim tht As New VSModel.CertifiedHouseholdAddress
                    tht.CertifiedID = p.CertifiedID
                    tht.TypeId = LoaiDiaChi.ThuongTru

                    tht.TinhId = (From h In Data.Provinces Where h.Title.ToUpper = _tinh.ToUpper Select h.Id).FirstOrDefault
                    'khi set = javascript se ko lay dc  phai set vao hidden field
                    tht.HuyenId = (From h In Data.Provinces Where h.Title.ToUpper = _Huyen.ToUpper Select h.Id).FirstOrDefault
                    tht.XaThiTranId = (From h In Data.Provinces Where h.Title.ToUpper = _Xa.ToUpper Select h.Id).FirstOrDefault
                    tht.Adress = p.Ow_Resident.ToString.Replace(_tinh, "").Replace(_Huyen, "").Replace(_Xa, "").Trim().TrimEnd(",").Trim().TrimEnd(",").Trim().TrimEnd(",").Trim().TrimEnd(",")

                    Data.CertifiedHouseholdAddresses.AddObject(tht)
                    Data.SaveChanges()

                    _tinh = _tamtru(_tamtru.Length - 1).Trim()
                    _Huyen = _tamtru(_tamtru.Length - 2).Trim()
                    If _tamtru.Length > 3 Then
                        _Xa = _tamtru(_tamtru.Length - 3).Trim()
                    Else
                        _Xa = ""
                    End If


                    'tam trú
                    Dim tt As New VSModel.CertifiedHouseholdAddress
                    tt.CertifiedID = p.CertifiedID
                    tt.TypeId = LoaiDiaChi.TamTru

                    tt.TinhId = (From h In Data.Provinces Where h.Title.ToUpper = _tinh.ToUpper Select h.Id).FirstOrDefault
                    'khi set = javascript se ko lay dc  phai set vao hidden field
                    tt.HuyenId = (From h In Data.Provinces Where h.Title.ToUpper = _Huyen.ToUpper Select h.Id).FirstOrDefault
                    tt.XaThiTranId = (From h In Data.Provinces Where h.Title.ToUpper = _Xa.ToUpper Select h.Id).FirstOrDefault
                    tt.Adress = p.Ow_Present.ToString.Replace(_tinh, "").Replace(_Huyen, "").Replace(_Xa, "").TrimEnd(",").Trim().TrimEnd(",").Trim().TrimEnd(",").Trim().TrimEnd(",").Trim().TrimEnd(",")

                    Data.CertifiedHouseholdAddresses.AddObject(tt)
                    Data.SaveChanges()

                    Dim _diadiemkd As String() = p.Biz_HeadOffice.ToString.Split(",")
                    _tinh = _tamtru(_tamtru.Length - 1).Trim()
                    _Huyen = _tamtru(_tamtru.Length - 2).Trim()
                    _Xa = _tamtru(_tamtru.Length - 3).Trim()

                    'địa điểm kinh doanh
                    Dim kd As New VSModel.CertifiedHouseholdAddress
                    kd.CertifiedID = p.CertifiedID
                    kd.TypeId = LoaiDiaChi.DiaDiemKinhDoanh
                    kd.TinhId = (From h In Data.Provinces Where h.Title = _tinh Select h.Id).FirstOrDefault
                    'khi set = javascript se ko lay dc  phai set vao hidden field
                    kd.HuyenId = (From h In Data.Provinces Where h.Title = _Huyen Select h.Id).FirstOrDefault
                    kd.XaThiTranId = (From h In Data.Provinces Where h.Title = _Xa Select h.Id).FirstOrDefault
                    kd.Adress = p.Biz_HeadOffice.ToString.Replace(_tinh, "").Replace(_Huyen, "").Replace(_Xa, "").TrimEnd(",").Trim().TrimEnd(",").Trim().TrimEnd(",").Trim().TrimEnd(",").Trim().TrimEnd(",")

                    Data.CertifiedHouseholdAddresses.AddObject(kd)
                    'update xaphuong cho dia diem kinh doanh
                    If Not kd.XaThiTranId Is Nothing Then
                        p.XaPhuongId = kd.XaThiTranId
                    End If

                    Data.SaveChanges()

                    If p.ParentId Is Nothing Then
                        Dim _parentId = (From k In Data.CertifiedHouseholds Where k.CertifiedCode = p.CertifiedCode And k.ParentId = 0 Select k.CertifiedID).FirstOrDefault
                        p.ParentId = _parentId
                        Data.SaveChanges()
                    End If
                    p.Ref2 = 1
                    Data.SaveChanges()
                Catch ex As Exception
                    p.Ref2 = 2
                    Data.SaveChanges()
                End Try

                'xu ly nganh nghe kinh doanh

                Dim nganhnghekinhdoanh As String = IIf(p.NganhNgheKinhDoanh Is Nothing, "", p.NganhNgheKinhDoanh)

                'If nganhnghekinhdoanh <> "" Then
                '    Dim _manganh = 
                'End If


                '        ddlTinhHoKhau.SelectedValue = ddlTinhHoKhau.Items.FindByText(_tinh).Value
                '        End If
                'If Not ddlHuyenHoKhau.Items.FindByText(_Huyen) Is Nothing Then
                '    ddlHuyenHoKhau.SelectedValue = ddlHuyenHoKhau.Items.FindByText(_Huyen).Value
                'End If
                'If Not ddlXaThiTranHoKhau.Items.FindByText(_Xa) Is Nothing Then
                '    ddlXaThiTranHoKhau.SelectedValue = ddlXaThiTranHoKhau.Items.FindByText(_Xa).Value
                'End If
                'txtDiaChiHoKhau.Text = p.Biz_HeadOffice


            End Using

        End If
    End Sub
#End Region

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        BindToGrid()
    End Sub
End Class
